const extend = require('extend');

const Dispatcher = require('./dispatcher');

class Role extends Dispatcher {
    constructor(server, room, player, team, index) {
        super();
        if (index < 0) throw '';
        this.server = server;
        this.room = room;
        this.player = player;
        this.team = team || 0;
        this.index = index || 0;
        this.isReady = false;
        this.group = this.team;
        this.position = this.index;
    }
    exit(focus = false) {
        this.room.exit(this, focus);
    }
    move({ team, index }) {
        this.room.move(this, team, index);
    }
	disable({ team, index }) {
		this.room.disable(this, team, index);
	}
	enable({ team, index }) {
		this.room.enable(this, team, index);
	}
    kick({ team, index }) {
        this.room.kick(this, team, index);
    }
    ready() {
        if (!this.isReady) {
            this.isReady = true;
            this.room.game.start();
        }
    }
    over(win, game) {
        this.isReady = false;
        return this.result = {
            win, title: win ? 'You win' : 'You lose'
        }
    }
    confirm() {
        delete this.result;
    }
    start() {

    }
    message(message) {
        if (this.room.game) {
            this.room.game.message(this, message);
        } else {
            this.room.message(this, message);
        }
    }
    get game() {
        if (this.room.game) {
            return this.room.game;
        } else {
            throw '';
        }
    }
    get board() {
        return this.game.board;
    }
    get summary() {
        return {
            player: {
                id: this.player.id,
                name: this.player.name,
                leaved: this.player.leaved,
                isReady: this.player.isReady
            }
        }
    }
    get data() {
        return extend(true, this.summary, {
            team: this.team,
            group: this.group,
            result: this.result
        });
    }
}

module.exports = Role;